﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统菜单仓储
/// </summary>
public class SysMenuRep : BaseRep<SysMenuModel>
{
    private static string sqlFields = string.Format(@"
         `menu_id`
        ,`menu_name`
        ,`parent_id`
        ,`order_num`
        ,`path`
        ,`component`
        ,`route_params`
        ,`is_frame`
        ,`is_cache`
        ,`menu_type`
        ,`visible`
        ,`status`
        ,`perms`
        ,`icon`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");

    /// <summary>
    /// 通过菜单编号获取系统菜单信息
    /// </summary>
    /// <param name="menuId">菜单编号</param>
    /// <returns>SysMenuModel?</returns>
    public SysMenuModel? GetSysMenuByMenuId(long menuId)
    {
        string sqlWhere = " and `menu_id` = @MenuId ";

        return QuerySingle(sqlFields, sqlWhere, new { MenuId = menuId });
    }

    /// <summary>
    /// 获取系统菜单列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysMenuModel></returns>
    public IEnumerable<SysMenuModel> GetSysMenuList(SysMenuModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.MenuName))
        {
            sqlWhere += " and `menu_name` like concat('%', @MenuName ,'%') ";
        }
        if (model.ParentId > -1)
        {
            sqlWhere += " and `parent_id` = @ParentId ";
        }
        if (model.IsFrame > -1)
        {
            sqlWhere += " and `is_frame` = @IsFrame ";
        }
        if (model.IsCache > -1)
        {
            sqlWhere += " and `is_cache` = @IsCache ";
        }
        if (!string.IsNullOrWhiteSpace(model.MenuType))
        {
            sqlWhere += " and `menu_type` = @MenuType ";
        }
        if (!string.IsNullOrWhiteSpace(model.Visible))
        {
            sqlWhere += " and `visible` = @Visible ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统菜单列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysMenuModel></returns>
    public Page<SysMenuModel> GetSysMenuPageList(SysMenuModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.MenuName))
        {
            sqlWhere += " and `menu_name` like concat('%', @MenuName ,'%') ";
        }
        if (model.ParentId > -1)
        {
            sqlWhere += " and `parent_id` = @ParentId ";
        }
        if (model.IsFrame > -1)
        {
            sqlWhere += " and `is_frame` = @IsFrame ";
        }
        if (model.IsCache > -1)
        {
            sqlWhere += " and `is_cache` = @IsCache ";
        }
        if (!string.IsNullOrWhiteSpace(model.MenuType))
        {
            sqlWhere += " and `menu_type` = @MenuType ";
        }
        if (!string.IsNullOrWhiteSpace(model.Visible))
        {
            sqlWhere += " and `visible` = @Visible ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过系统用户编号获取系统菜单列表
    /// </summary>
    /// <param name="userId">用户编号</param>
    /// <returns>IEnumerable<SysMenuModel></returns>
    public IEnumerable<SysMenuModel> GetSysMenuListByUserId(long userId)
    {
        string sqlStr = string.Format(@"
            select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
            from sys_menu m
            left join sys_role_menu rm on m.menu_id = rm.menu_id
            left join sys_user_role ur on rm.role_id = ur.role_id
            left join sys_role ro on ur.role_id = ro.role_id
            where ur.user_id = @UserId
        ");

        return Query(sqlStr, new { UserId = userId });
    }

    /// <summary>
    /// 通过菜单编号更新系统菜单信息
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysMenuByMenuId(SysMenuModel model)
    {
        string sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.MenuName))
        {
            sqlSet += " `menu_name` = @MenuName, ";
        }

        if (model.ParentId > -1)
        {
            sqlSet += " `parent_id` = @ParentId, ";
        }

        if (model.OrderNum > -1)
        {
            sqlSet += " `order_num` = @OrderNum, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Path))
        {
            sqlSet += " `path` = @Path, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Component))
        {
            sqlSet += " `component` = @Component, ";
        }

        if (!string.IsNullOrWhiteSpace(model.RouteParams))
        {
            sqlSet += " `route_params` = @RouteParams, ";
        }

        if (model.IsFrame > -1)
        {
            sqlSet += " `is_frame` = @IsFrame, ";
        }

        if (model.IsCache > -1)
        {
            sqlSet += " `is_cache` = @IsCache, ";
        }

        if (!string.IsNullOrWhiteSpace(model.MenuType))
        {
            sqlSet += " `menu_type` = @MenuType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Visible))
        {
            sqlSet += " `visible` = @Visible, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Perms))
        {
            sqlSet += " `perms` = @Perms, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Icon))
        {
            sqlSet += " `icon` = @Icon, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `menu_id` = @MenuId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统菜单编号删除系统菜单
    /// </summary>
    /// <param name="menuId">系统菜单编号</param>
    /// <returns>int</returns>
    public int DeleteSysMenuByMenuId(long menuId)
    {
        string sqlWhere = " and `menu_id` = @MenuId ";

        return Delete(sqlWhere, new { MenuId = menuId });
    }
}

